<template>
  <BasicContainer>
    <div class="comment">
      <!-- 全部评论 -->
      <div class="comment_hd">
        <div class="allcomment">全部评论</div>
        <div class="border"></div>
      </div>
      <!-- 评论列表 -->
      <div
        class="comment_list"
        v-for="(item, index) in response"
        :key="item.comment_id + index"
      >
        <!-- 头像区域 -->
        <div class="commnet_image">
          <img class="img" :src="item.avatar" mode="" />
        </div>
        <!-- 昵称区域 -->
        <div class="comment_nickname">
          <div class="nickname_left">
            <div class="nickname">
              {{ item.nickname }}
            </div>
            <div v-if="item.is_author == 1" class="author">作者</div>
            <!-- 时间区域 -->
            <div class="commnet_time">{{ item.date | dateFormat }}</div>
          </div>
          <!-- 评论区域 -->
          <div class="nickname_center">{{ item.text }}</div>
          <!-- 点赞数 -->
          <div class="nickname_right">
            <img src="@/static/love.png" alt="" />
            {{ item.digg_num }}
          </div>
          <!-- 评论列表 -->
          <List :list="item.sub_comment" :more="item.more"></List>
        </div>
      </div>
    </div>
  </BasicContainer>
</template>

<style lang="scss" scoped>
</style>

<script>
import BasicContainer from "@/components/BasicContainer";
import List from "./list.vue";
export default {
  components: {
    BasicContainer,
    List,
  },
  data() {
    return {
      response: [],
    };
  },
  methods: {
    load() {
      this.getList();
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      setTimeout(() => {
        loading.close();
      }, 2000);
    },
    async getList() {
      const response = await this.$http.get(
        "http://douyin.bytedance.com/api/comment/list"
      );
      if (response.data.status != 0) {
        this.$message.warning({
          message: "评论列表获取失败请稍后再试",
          duration: 2000,
        });
        return false;
      }
      response.data.comments.forEach((item) => {
        if (item.sub_comment.length > 3) {
          item.more = true;
        } else {
          item.more = false;
        }
      });
      this.response = this.response.concat(response.data.comments);
      // console.log(this.response);
    },
  },
  mounted() {
    this.getList();
    let that = this;
    this.$nextTick(() => {
      window.onscroll = () => {
        var scrollTop =
          document.documentElement.scrollTop || document.body.scrollTop;
        var windowHeight =
          document.documentElement.clientHeight || document.body.clientHeight;
        var scrollHeight =
          document.documentElement.scrollHeight || document.body.scrollHeight;
        if (scrollTop + windowHeight == scrollHeight) {
          that.load();
        }
      };
    });
  },
};
</script>
<style lang="scss" scoped>
.comment {
  height: 100%;
  width: 100%;
  .comment_hd {
    margin: 0 0 40px 40px;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    .allcomment {
      color: #b3b3b5;
      font-size: 22px;
      // font-weight: bold;
      width: 120px;
    }
    .border {
      width: 100%;
      border-bottom: 1px solid #f1f2f5;
    }
  }
  .comment_list {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    margin: 20px 0;
    .commnet_image {
      margin-left: 40px;
      align-self: flex-start;
      .img {
        width: 70px;
        height: 70px;
        border-radius: 50%;
      }
    }
    .comment_nickname {
      flex: 1;
      margin: 0 20px;
      padding: 10px 0;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      border-bottom: 1px solid #f1f2f5;
      .nickname_left {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        position: relative;
        align-items: center;
        .nickname {
          color: #6d6e71;
          font-size: 18px;
          cursor: pointer;
          max-width: 150px;
          overflow: hidden;
          text-overflow: ellipsis;
          word-break: break-all;
          white-space: nowrap;
        }
        .nickname:hover {
          max-width: 500px;
        }
        .author {
          color: #fff;
          background: #fb3159;
          padding: 5px 8px;
          line-height: 16px;
          font-size: 18px;
          margin: 0 20px;
          border-radius: 4px;
        }
        .commnet_time {
          position: absolute;
          right: 0;
          width: 120px;
          align-self: flex-end;
          align-items: flex-end;
          align-content: flex-end;
          color: #d8d8d9;
          font-size: 18px;
          text-align: right;
        }
      }

      .nickname_center {
        font-size: 20px;
        color: #2f3035;
        margin: 15px 0;
      }
      .nickname_right {
        vertical-align: middle;
        display: flex;
        flex-direction: row;
        align-items: center;
        color: #6d6e71;
        font-size: 16px;
        img {
          width: 20px;
          margin-right: 10px;
        }
      }
    }
  }
}
</style>